// pages/pickup/pickup.js
var util = require("../../../utils/util.js");
var api = require("../../../config/api.js");
var pick = require("../../../modules/pickup_module.js");
var address = require("../../../modules/saveAddress.js");
const App = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        searchWord: "",
		pickupList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            searchWord: options.wd,
        });
		this.getData();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    // 选中提货点
    radioChange: async function (e) {
		console.log(e.detail.value);
		const list = this.data.pickupList;
		const item = list.length && list.find(item=> item.id == e.detail.value) || {};
		App.globalData.memberLeaderObj = item
        await address._saveAddress(item.id);
		wx.navigateBack({
			delta: 2,//返回到选择提货点的触发点
		})
	},
	// 获取初始数据
    getData() {
        const _this = this;
        const memberLeaderObj = App.globalData.memberLeaderObj;
		console.log("memberLeaderObj", memberLeaderObj);
        pick._getPickupData({storeName:_this.data.searchWord}).then(data=>{
            data.length && data.forEach((item, index, arr)=>{
                if(memberLeaderObj.id === item.id){
                    arr[index].isCheked = true;
                }else{
                    arr[index].isCheked = false;
                }
            })
            _this.setData({
                pickupList: data,
                currentPickup: memberLeaderObj.id ? memberLeaderObj : {}
            })
        })
    },
});
